<?php

class ErrorClass {

	static private $errors;

	static public function add($data, $log = true, $auto_die = true)
	{
		self::$errors[] = $data;

		echo self::display($data['type']);

		if($log && class_exists('CW_LogClass'))
		{
			CW_LogClass::add($data['message'], $data['file'], $data['line']);
		}

		if($auto_die)
		{
			die();
		}

		return true;
	}

	static public function clear()
	{
		self::$errors = array();

		return true;
	}

	static public function count()
	{
		return sizeof(self::$errors);
	}

	static public function display($type = 'core')
	{
		$output = self::getHeader();

		foreach(self::$errors as $key => $error)
		{
			switch($type)
			{
				case 'database':
				case 'xsl':
				case 'core':
				case 'file':

					$output .= self::displayFile($key + 1, $error);
					break;
			}
		}

		$output .= self::getFooter();

		die($output);
	}

	static private function displayFile($key, $error)
	{
		$file = end(explode(DIRECTORY_SEPARATOR, $error['file']));

		$output  = "<h2>Error #{$key} found in {$file} at or around line {$error['line']}:</h2>";
		$output .= "<h3>- the full path is {$error['file']}</h3>";
		$output .= "<p>{$error['message']}</p>";
		$output .= '<div class="code-wrapper">';

		$source = file($error['file']);

		for($i = $error['line'] - 6; $i <= $error['line'] + 6; $i++)
		{
			if($i >= 1 && $i <= sizeof($source))
			{
				$line = str_replace(array("\t", ' '), array('&nbsp;&nbsp;&nbsp;&nbsp;', '&nbsp;'), @rtrim(htmlentities($source[$i - 1])));

				$gutter = sprintf("%03d", $i);

				if(false == trim($line))
				{
					$line = '&nbsp;';
				}

				if($i == $error['line'])
				{
					$output .= "<div><code><strong>{$gutter}</strong> <span>{$line}</span></code></div>\n";
				}
				else if(trim($line) && $error['line'] - 1 == $i || $error['line'] + 1 == $i)
				{
					$output .= "<div><code><strong>{$gutter}</strong> <span class=\"faded\">{$line}</span></code></div>\n";
				}
				else
				{
					$output .= "<div><code><strong>{$gutter}</strong> {$line}</code></div>\n";
				}
			}
		}

		$output .= "</div>\n";

		return $output;
	}

	static private function displayXsl()
	{
	}

	static private function displayCore()
	{
	}

	static private function displayDatabase()
	{
	}

	static private function getHeader()
	{
		ob_start();

		?>

		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
				<title><?php echo self::count(); ?> Error(s) Encountered!</title>
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
				<style type="text/css">

					body {
						background: #fff;
						padding: 0 25px;
						font: normal normal 1em/1.6em Arial, sans-serif;
						color: #000;
					}

					h1 {
						font-weight: normal;
						padding-bottom: 10px;
						border-bottom: 2px solid #eee;
						color: #555;
						margin: 10px 0;
					}

					h2 {
						color: #c00;
						font-size: 14px;
						margin-bottom: 0;
						padding-bottom: 0;
					}

					h3 {
						padding: 0;
						margin: 0;
						line-height: 1em;
					}

					h4 {
						font-size: 12px;
						font-weight: normal;
						margin: 0;
						padding: 0;
					}

					p {
						margin: 5px 0 10px 0;
					}

					h3 {
						font-size: 11px;
						color: #888;
						font-weight: normal;
					}

					code {
						display: block;
						color: #fff;
						font-size: 11px;
						padding: 0 10px;
					}

					code strong {
						color: #888;
						padding: 2px;
					}

					code span {
						color: #fff;
						padding: 2px;
						background: #333 none;
					}

						code span.faded{
							background: #222 none;
						}

					.code-wrapper {
						background: #000 none;
						color: #fff;
						overflow: auto;
					}

					.code-wrapper div {
						border-bottom: 1px solid #333;
					}

					#footer-wrapper {
						border-top: 1px solid #ddd;
						margin-top: 10px;
						overflow: hidden;
						font-size: .8em;
						color: #888;
						padding: 0 10px;
						background: #eee none;
					}

					#footer-wrapper p {
						margin: 0;
						padding: 0;
					}

						#footer-wrapper p.left {
							float: left;
						}

						#footer-wrapper p.right {
							float: right;
						}

					#footer-wrapper a {
						color: #888;
						text-decoration: underline;
					}

					#footer-wrapper a:hover {
						color: #444;
					}

				</style>
			</head>
			<body>
				<h1><?php echo self::count(); ?> Error(s) Encountered!</h1>
		<?php

		$header = ob_get_contents();

		ob_end_clean();

		return $header;
	}

	static private function getFooter()
	{
		ob_start();

		?>
				<div id="footer-wrapper">
					<p class="left">Powered by <a href="http://www.getcogsworth.com/" title="Cogsworth">Cogsworth</a> &copy; 2008</p>
					<p class="right">Engineered by <a href="http://www.thedrunkenepic.com" title="Daniel Wilhelm II Murdoch">Daniel Wilhelm II Murdoch</a></p>
				</div>
			</body>
		</html>

		<?php

		$footer = ob_get_contents();

		ob_end_clean();

		return $footer;
	}
}

function CogsworthErrorHandler($type, $message, $file, $line)
{
	if(in_array($type, array(E_WARNING, E_NOTICE)))
	{
		return ErrorClass::add(array('message' => $message, 'file' => $file, 'line' => $line, 'type' => 'core'));
	}

	return true;
}

?>